#include "XMLSettings.h"

XMLSettings::XMLSettings() /*: logger( "XMLSettings", INFO )*/
{
	levelNamesXML[ 0 ] = "INFO";
	levelNamesXML[ 1 ] = "WARN";
	levelNamesXML[ 2 ] = "ERROR";
	levelNamesXML[ 3 ] = "CRITICAL";
};

XMLSettings::~XMLSettings()
{

}

XMLSettings& XMLSettings::getInstance()
{
	static XMLSettings settings;
	return settings;
}

void XMLSettings::readXML()
{
	xml_parser::read_xml( "..//XML//properties.xml", properties );

	resolution.h = properties.get( "application.graphics.resolution.height", 600 );
	resolution.w = properties.get( "application.graphics.resolution.width", 800 );
	bitDepth = properties.get( "application.graphics.resolution.bit_depth", 8 );
	dirButtons[0] = static_cast<SDLKey>(properties.get("application.controls.movement.up", 273));
	dirButtons[1] = (SDLKey)properties.get("application.controls.movement.down", 274);
	dirButtons[2] = (SDLKey)properties.get("application.controls.movement.left", 276);
	dirButtons[3] = (SDLKey)properties.get("application.controls.movement.right", 275);
	memcpy_s(serverIP, 32, properties.get("application.network.server", "localhost").c_str(), 32);
}

// Takes in the class name to get the logger level 
Level XMLSettings::findLoggerLevel( const std::string name )
{
	// loads the xml to read into properties
	xml_parser::read_xml( "..//XML//properties.xml", properties );

	// Uses the name sent in the get the full path to the logger level in the xml file
	std::string filePath = "application.logger.logger_levels.";
	filePath += name;
	
	// Gets the logger lever back as a string, stored in filePath
	std::string loggerLevel = properties.get( filePath, "NULL" );
	
	// If the logger level returned was NULL...
	if(  loggerLevel.compare( "NULL" ) == 0 )
	{
		std::cout << "logger level for something not found" << std::endl;
		return INFO;
	}
	
	// Compares the string we got back with the different logger levels and decides
	// which enum to return based on the string
	if( loggerLevel.compare( levelNamesXML[ 0 ] ) == 0 )
	{
		return INFO;
	}else if( loggerLevel.compare( levelNamesXML[ 1 ] ) == 0 )
	{
		return WARN;
	}else if( loggerLevel.compare( levelNamesXML[ 2 ] ) == 0 )
	{
		return ERROR;
	}else if( loggerLevel.compare( levelNamesXML[ 3 ] ) == 0 )
	{
		return CRITICAL;
	}

	return INFO;
}

// The trigger level is the minimun level a logger level can be to be written out
// same stuff as above
Level XMLSettings::getTriggerLevel()
{
	xml_parser::read_xml( "..//XML//properties.xml", properties );

	std::string level = properties.get( "application.logger.trigger_level", "NO_LEVEL" );

	if(  level.compare( "NO_LEVEL" ) == 0 )
	{
		std::cout << "trigger level not found" << std::endl;
		return INFO;
	}

	if( level.compare( levelNamesXML[ 0 ] ) == 0 )
	{
		return INFO;
	}else if( level.compare( levelNamesXML[ 1 ] ) == 0 )
	{
		return WARN;
	}else if( level.compare( levelNamesXML[ 2 ] ) == 0 )
	{
		return ERROR;
	}else if( level.compare( levelNamesXML[ 3 ] ) == 0 )
	{
		return CRITICAL;
	}

	return INFO;
}

Resolution XMLSettings::getResolution()
{
	return resolution;
}

int XMLSettings::getBitDepth()
{
	return bitDepth;
}

SDLKey* XMLSettings::getDirButtons()
{
	return dirButtons;
}


char* XMLSettings::getServerIP()
{
	return serverIP;
}
